package com.example.myapplication.View;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.Base.BaseActivity;
import com.example.myapplication.R;

public class BroadCastActivity extends BaseActivity {

    private IntentFilter intentFilter;
    private NetWorkChangeReceiver netWorkChangeReceiver;
    private LocalReceiver localReceiver;
//    private LocalBroadcastManager localBroadcastManager; //不用不知道，原来这个localbroadcastmanager已经废弃了
    private static final String TAG = "BroadCastActivity";

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.broad_cast_activity);
        Log.d(TAG, "onCreate: get int BroadCastActivity page");
        intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        netWorkChangeReceiver = new NetWorkChangeReceiver();
        registerReceiver(netWorkChangeReceiver, intentFilter);
        Button button = (Button) findViewById(R.id.send_broad_button);
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Log.d(TAG, "onClick: sendBroadCast");
                Intent intent = new Intent("com.example.broadcast.MY_BROADCAST");
                sendBroadcast(intent);
            }
        });
        Button button1 = (Button) findViewById(R.id.send_order_broad_button);
        button1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Intent intent = new Intent("com.exmaple.broadcast.MY_ANOTHERBROADCAST");
                sendOrderedBroadcast(intent, null);
            }
        });
        Button button2 = (Button) findViewById(R.id.force_off_line_button);
        button2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Intent intent = new Intent("com.example.broadcastbestpractice.FORCE_OFFLINE");
                sendBroadcast(intent);
            }
        });
    }


    @Override
    public void onDestroy(){
        super.onDestroy();
        unregisterReceiver(netWorkChangeReceiver);
    }

    public class NetWorkChangeReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent){
            Toast.makeText(context, "network changes", Toast.LENGTH_LONG).show();
            ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.isAvailable()){
                Toast.makeText(context, "network is avaliable", Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(context, "net work is unavaliable", Toast.LENGTH_LONG).show();
            }
        }
    }

    public class LocalReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent){
            Toast.makeText(context, "receiver local broad", Toast.LENGTH_LONG).show();
        }
    }

}
